//
//  ViewController.m
//  HZKitDemo
//
//  Created by 红纸 on 16/6/15.
//  Copyright © 2016年 HongZhi. All rights reserved.
//

#import "ViewController.h"
#import <Masonry/Masonry.h>

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView* mainTableView;
@property (nonatomic, strong) NSArray* viewControllerNameArray;

@end

@implementation ViewController

#pragma mark - Life Cycle

- (instancetype)init{
    if (self = [super init]) {
        [self p_initAll];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"MainPage";
    
    _mainTableView = ({
        UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.view addSubview:tableView];
        tableView;
    });
    
    [_mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

#pragma mark - Private Method

- (void)p_initAll{
    _viewControllerNameArray = @[@[@"HZKitMarcoViewController"]];
}

#pragma mark - TableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _viewControllerNameArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_viewControllerNameArray[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* identifier = @"cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [[_viewControllerNameArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    Class cls = NSClassFromString(_viewControllerNameArray[indexPath.section][indexPath.row]);
    id obj = [[cls alloc] init];
    [(UIViewController *)obj setTitle:_viewControllerNameArray[indexPath.section][indexPath.row]];
    [self.navigationController pushViewController:obj animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
